<?php
namespace App\Service\Front;

use Illuminate\Support\Facades\DB;

class YewuService extends BaseService
{
    //业务

    //业务列表
    public function getIndex($type, $pages)
    {
        $start = ($pages['start'] - 1) * $pages['limit'];
        $db = $this->dbConn($this->dbYewu)
            ->where('type', $type);
        $total = $db->count();
        $res = $db->skip($start)->take($pages['limit'])
            ->get();
        if (!count($res)) {
            return $this->toback(-2, '没有记录');
        }
        $arys = [];
        foreach ($res as $k => $v) {
            $v = (array) $v;
            $jsons = json_decode($v['json'], true);
            $yewu = $jsons;
            $yewu['id'] = $v['id'];
            $yewu['type'] = $v['type'];
            $arys[] = $yewu;
        }
        return $this->toback(0, '', $arys, $total);
    }

    //一个
    public function getOne($id)
    {
        $res = (array) $this->dbConn($this->dbYewu)->where('id', $id)->first();
        if (empty($res)) {
            return array('code' => -2, 'msg' => '没有记录');
        }
        $yewu = json_decode($res['json'], true);
        $yewu['id'] = $id;
        $yewu['type'] = $res['type'];
        return array('code' => 0, 'msg' => 'success', 'data' => $yewu);
    }

    //添加
    public function add($type, $pas)
    {
        $arys = [
            'type' => $type,
            'json' => json_encode($pas, JSON_UNESCAPED_UNICODE),
            'addtime' => toDate(),
        ];
        $lastid = $this->dbConn($this->dbYewu)->insertGetId($arys);
        if (!$lastid) {
            return $this->toback(-1, '添加失败');
        }
        return $this->toback();
    }
}